package com.lantosec.hmrp.modules.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.category.entity.AssetsSupplierDirectory;
import com.lantosec.hmrp.modules.category.mapper.AssetsSupplierDirectoryMapper;
import com.lantosec.hmrp.modules.category.service.IAssetsSupplierDirectoryService;
import com.lantosec.hmrp.modules.category.vo.AssetsSupplierDirectoryVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商及厂家目录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-31
 */
@Service
@Primary
@Log4j2
public class AssetsSupplierDirectoryServiceImpl extends ServiceImpl<AssetsSupplierDirectoryMapper, AssetsSupplierDirectory> implements IAssetsSupplierDirectoryService {
    @Override
    public Page<AssetsSupplierDirectory> queryPage(PageVO<AssetsSupplierDirectory> page, AssetsSupplierDirectoryVO condition) {
        PageVO<AssetsSupplierDirectory> param = baseMapper.selectPage(page, new QueryWrapper<AssetsSupplierDirectory>()
                .eq(StringUtils.isNotEmpty(condition.getSupplierId()), "supplier_id", condition.getSupplierId())
                .like(StringUtils.isNotEmpty(condition.getSupplier()), "supplier", condition.getSupplier()));
        return page.setRecords(param.getRecords());
    }

    @Override
    public String create(String name) {
        AssetsSupplierDirectory info = new AssetsSupplierDirectory();
        String supplierId = baseMapper.selectMaxSupplierId();
        if(StringUtils.isEmpty(supplierId)){
            supplierId = "SUP0001";
        }
        //供应商ID
        info.setSupplierId(supplierId);
        if(StringUtils.isNotEmpty(name)){
            info.setSupplier(name);
        }
        info.setManualFlag(1);
        baseMapper.insert(info);
        return supplierId;
    }

    @Override
    public List fuzzyQuery(String supplierName) {
        List<AssetsSupplierDirectory> name = baseMapper.selectList(new QueryWrapper<AssetsSupplierDirectory>()
                .like(StringUtils.isNotEmpty(supplierName), "supplier", supplierName));
        return name;
    }
}
